'use strict';

const electron = require('electron');
const svc = require('./backend/service');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const shell = electron.shell;
const ipcMain = require('./backend/regierIpc');
const ipcFun = require('./backend/ipcCallback');
const login = require('./backend/login');
global.cookies = {};
global.access_token = {};
global.topWindow = null;
global.childWindow = null;
global.loginWindow = null;
global.is_login = true;
global.login_url = 'file://' + __dirname + '/tk_login.html';
global.tk_is_login = false;
global.cookies.tk_cookie = {"_uab_collina":"148877700796093911081775","t":"37ca0dc889eb3115e8c3d63c77dc2741","cna":"rM5DEc7MaGQCAbaWP8OtSuMQ","_umdata":"535523100CBE37C397194EE85A70A2476B3C033D9ADE116789C7F6FBCC3E2C011DB7B5012EEDB133CD43AD3E795C914C52A351D1F17CCFD1427D6AF0F1191E43","l":"AsHBPVqKGjlZM4SkP6/1qpav0Qbb7jXg","isg":"AomJ5LuBNepWb8lwewF_rZg9k7W7cH0IqkoN9Sv-BXCvcqmEcyaN2Hcg4my7","cookie2":"cdad4b4a0b288a5a918cc377b788b363","_tb_token_":"FvIi1Ca6p3Vq","v":"0","cookie32":"7646341f4e8796714a719063e093ef2b","cookie31":"MjY5MDc5NDYsJUU1JUE0JUE5JUU0JUJEJUJGJUU0JUI4JThFJUU5JUFEJTk0JUU5JUFDJUJDJUU3JUJBJUE2JUU0JUJDJTlBLDQ1NDg3MDI0N0BxcS5jb20sVEI%3D","alimamapwag":"TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgdHJhZGluZy1hcHAvMC4xLjAgQ2hyb21lLzQ3LjAuMjUyNi4xMTAgRWxlY3Ryb24vMC4zNi4xMiBTYWZhcmkvNTM3LjM2","login":"Vq8l%2BKCLz3%2F65A%3D%3D","alimamapw":"EnJxRCBxEiB3HCNYFSFSEHd6EnIGRCQFEiEPHCQlFSFVEHB7EnNwRCADPlMDXVVXUVJWBVANAAgA%0AAgMCVFABClIDAVJQUwYJU1RQ"};
global.start_applf = false;
global.procss = null;
var myWindow = null;

app.on('window-all-closed', function () {
    if (process.platform != 'darwin') {
        app.quit();
    }
});

app.on('ready', function () {
    var loginWindow = login.init('file://' + __dirname + '/login.html');
    global.loginWindow = loginWindow.id;
    loginWindow.on('close', function () {
        if (global.is_login) {
            myWindow = svc.init();
            var url = 'file://' + __dirname + '/index.html';
            svc.createWidonw(url);
            myWindow.show();
            var w = svc.getWebContent();
            // Open the DevTools.
            w.openDevTools();
            global.topWindow = myWindow.id;
            // Emitted when the window is closed.
            myWindow.on('closed', function () {
                app.quit();
                myWindow = null;
                global.topWindow = null;
                svc.clearWindow();
            });

            myWindow.on('focus', function () {
                if (global.childWindow !== null) {
                    var childWindow = BrowserWindow.fromId(global.childWindow);
                    childWindow.focus();
                }
                return true;
            });

            w.on('will-navigate', function (event) {
                svc.hanldeWillNavigate();
                event.preventDefault();
            });
            w.on('new-window', function (event, url) {
                event.preventDefault();
            })
        }
        return false;
    });
    var l_w = loginWindow.webContents;
    l_w.on('new-window', function (event, url) {
        console.log(url);
        shell.openExternal(url);
        event.preventDefault();
    });
    // l_w.openDevTools();
});

var shouldQuit = app.makeSingleInstance(function (commandLine, workingDirectory) {
    if (myWindow) {
        if (myWindow.isMinimized()) myWindow.restore();
        myWindow.focus();
    }
});
if (shouldQuit) {
    app.quit();
    return;
}
ipcMain.regServer({
    'login':ipcFun.login,
    'get_ses': ipcFun.onWindowChange,
    'getOs': ipcFun.onGetOsInfo,
    'openWindow': ipcFun.onOpenWindw,
    'accessToken': ipcFun.accessToken,
    'tkLogin': ipcFun.tkLogin,
    'minLogin': ipcFun.minLogin,
    'applfCommin': ipcFun.applfCommis,
    'stopApplf': ipcFun.stopProcess
});